/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.Pronosticos;

import Entity.DatosMetodosPronosticos;
import Entity.Periodo;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import org.jfree.data.xy.XYSeries;

/**
 *
 * @author camilourd
 */
public class MetodosPronosticos {
    DatosMetodosPronosticos dmp;

    public MetodosPronosticos() {
        dmp = new DatosMetodosPronosticos();
    }
    
    public boolean isValidConstant(double var) {
        return (var >= 0.0 && var <= 1.0);
    }
    
    public boolean isValidDemand(double var) {
        return (var >= 0.0);
    }
    
    public void calcularErrores(double ytn, double ypn, double n) {
        if(ytn != 0.0 && n != 0.0) {
            dmp.setEr(ytn - ypn);
            dmp.setDam(Math.abs((ytn - ypn) / n));
            dmp.setEmc(((ytn - ypn) * (ytn - ypn)) / n);
            dmp.setPem((((ytn - ypn) / ytn) / n) * 100.0);
            dmp.setPema(Math.abs(dmp.getPem()));
        }
    }
    
    public void obtenerYp(javax.swing.JTable table, javax.swing.JTextField txtA, javax.swing.JTextField txtB) {
        ArrayList<Double> yp = dmp.getYp();
        int n = yp.size();
        
        String[] columnNames = {"Periodo", "Demanda"};
        Object[][] data = new Object[n][2];
        table.setModel(new DefaultTableModel(data, columnNames));
        
        for(int i = 0;i < n; ++i) {
            table.setValueAt("Periodo " + i, i, 0);
            table.setValueAt(aproxDouble(yp.get(i)), i, 1);
        }
        
        txtA.setText(aproxDouble(dmp.getA()));
        txtB.setText(aproxDouble(dmp.getB()));
    }
    
    public void obtenerDatosGrafico(XYSeries seriesYt, XYSeries seriesYp, XYSeries seriesTend) {
        ArrayList<Periodo> pers = dmp.getPers();
        int n = pers.size();
        double a = 0.0, b = 0.0, sumx = 0.0, sumy = 0.0, c = 0.0, d = 0.0;
        
        for(int i = 0; i < n; ++i) {
            seriesYt.add(i + 1, pers.get(i).getDemanda());
            a += (i + 1) * (pers.get(i).getDemanda());
            sumx += i + 1;
            sumy += pers.get(i).getDemanda();
            c += (i + 1) * (i + 1);
        }
        
        ArrayList<Double> yp = dmp.getYp();
        int xp = yp.size();
        
        for(int i = 0;i < xp; ++i) {
            seriesYp.add(i + n, yp.get(i));
            if(i > 0) {
                a += (i + n) * (yp.get(i));
                sumx += i + n;
                sumy += yp.get(i);
                c += (i + n) * (i + n);
            }
        }
        
        double t = xp + n - 1;
        a *= t;
        b = sumx * sumy;
        d = sumx * sumx;
        c *= t;
        double m = (a - b) / (c - d);
        double e = sumy;
        double f = sumx * m;
        b = (e - f) / t;
        
        seriesTend.add(1, m + b);
        seriesTend.add(t, (m * t) + b);
    }
    
    public String aproxDouble(double data) {
        return String.format("%.3f", data);
    }
    
    public void addAlpha(double data) {
        dmp.setAlpha(data);
    }
    
    public void addBeta(double data) {
        dmp.setBeta(data);
    }
    
    public void addGamma(double data) {
        dmp.setGamma(data);
    }
    
    public String obtenerEr() {
        return aproxDouble(dmp.getEr());
    }
    
    public String obtenerEmc() {
        return aproxDouble(dmp.getEmc());
    }
    
    public String obtenerDam() {
        return aproxDouble(dmp.getDam());
    }
    
    public String obtenerPem() {
        return aproxDouble(dmp.getPem());
    }
    
    public String obtenerPema() {
        return aproxDouble(dmp.getPema());
    }
    
    public String obtenerAlpha() {
        return aproxDouble(dmp.getAlpha());
    }
    
    public String obtenerBeta() {
        return aproxDouble(dmp.getBeta());
    }
    
    public String obtenerGamma() {
        return aproxDouble(dmp.getGamma());
    }
}
